from rest_framework.authentication import BasicAuthentication, TokenAuthentication
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response

from api.serializers.wish_product_serializer import WishProductSerializer


@api_view(['POST'])
@authentication_classes((BasicAuthentication, TokenAuthentication))
@permission_classes((IsAuthenticated,))
def add_wish_product(request: Request):
    serializer = WishProductSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save(user=request.user)
        return Response({'detail': u'添加成功'})
    else:
        raise ValidationError(serializer.errors)
